/**
 *  This file is part of OhSnap.
 *
 * 	Copyright (c) 2009 Gabriel Mendonça and Marcelo Machado
 * 
 *  OhSnap is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OhSnap is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OhSnap.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package br.mm.ui;

import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.mm.cg.OhSnap;

import com.bric.swing.ColorPicker;

/**
 * @author machado
 * 
 */

public class MainScreen extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton jButtonColor = null;
	private JButton jButtonBrowse = null;
	private Color color; // @jve:decl-index=0:
	private JPanel jPanel2 = null;
	private JLabel jLabelGeneral1 = null;
	private JCheckBox jCheckBox3D = null;
	private JLabel jLabelGeneral4 = null;
	private JTextField jTextFieldFile = null;
	private JButton jButtonRun = null;

	/**
	 * This is the default constructor
	 */
	public MainScreen() {
		super();
		initialize();
	}

	public String getMidiFile() {
		return jTextFieldFile.getText();
	}
	
	/**
	 * @return
	 */
	public float[] getColor() {
		return new float[] {color.getRed(), color.getGreen(), color.getBlue()};
	}

	/**
	 * @return <code>true</code> if the checkbox "3D" is checked. 
	 */
	public boolean get3d() {
		return jCheckBox3D.isSelected();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(585, 208);
		this.setContentPane(getJPanel2());
		this.setTitle("Oh Snap!");
		this.color = new Color(100, 150, 200);
	}
	
	/**
	 * This method initializes jButtonColor
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonColor() {
		if (jButtonColor == null) {
			jButtonColor = new JButton();
			jButtonColor.setText("Cor inicial");
			jButtonColor.setBounds(new Rectangle(42, 74, 172, 29));
			jButtonColor.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					pickColor();
				}
			});
		}
		return jButtonColor;
	}

	/**
	 * This method initializes jButtonBrowse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonBrowse() {
		if (jButtonBrowse == null) {
			jButtonBrowse = new JButton();
			jButtonBrowse.setText("Pesquisar");
			jButtonBrowse.setBounds(new Rectangle(430, 19, 112, 25));
			jButtonBrowse
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {

							browseFile();

						}
					});
		}
		return jButtonBrowse;
	}

	private void browseFile() {

		final JFileChooser fc = new JFileChooser();
		String name = new String();
		fc.addChoosableFileFilter(new MidiFilter());
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			name = file.getAbsolutePath();
		}

		jTextFieldFile.setText(name);

	}

	private void pickColor() {
		color = ColorPicker.showDialog(new JFrame(), color);
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jLabelGeneral4 = new JLabel();
			jLabelGeneral4.setBounds(new Rectangle(35, 20, 101, 23));
			jLabelGeneral4.setText("Arquivo MIDI");
			jLabelGeneral1 = new JLabel();
			jLabelGeneral1.setBounds(new Rectangle(44, 119, 56, 31));
			jLabelGeneral1.setText("3D?");
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.add(jLabelGeneral1, null);
			jPanel2.add(getJCheckBox3D(), null);
			jPanel2.add(jLabelGeneral4, null);
			jPanel2.add(getJTextFieldFile(), null);
			jPanel2.add(getJButtonBrowse(), null);
			jPanel2.add(getJButtonRun(), null);
			jPanel2.add(getJButtonColor(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jCheckBox3D	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox3D() {
		if (jCheckBox3D == null) {
			jCheckBox3D = new JCheckBox();
			jCheckBox3D.setBounds(new Rectangle(111, 121, 25, 26));
		}
		return jCheckBox3D;
	}

	/**
	 * This method initializes jTextFieldFile	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldFile() {
		if (jTextFieldFile == null) {
			jTextFieldFile = new JTextField();
			jTextFieldFile.setBounds(new Rectangle(150, 18, 267, 27));
			jTextFieldFile.setText("data/lullaby.mid");
		}
		return jTextFieldFile;
	}

	/**
	 * This method initializes jButtonRun	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonRun() {
		if (jButtonRun == null) {
			jButtonRun = new JButton();
			jButtonRun.setBounds(new Rectangle(348, 75, 157, 67));
			jButtonRun.setText("Rodar!!!");
			jButtonRun.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OhSnap.runAnimation();
				}
			});
		}
		return jButtonRun;
	}

}  //  @jve:decl-index=0:visual-constraint="104,-16"
